var Connect = YAHOO.util.Connect,
Panel = YAHOO.widget.Panel,
KeyListener = YAHOO.util.KeyListener,
Event = YAHOO.util.Event,
Dom = YAHOO.util.Dom,
Json = YAHOO.lang.JSON;
var currentPage = 1;
var countItem = 1;
var MyController = {

    asyncRequestFalse:function(){
        alert('AsyncRequest False');
    },
    
    addProductToShopCat:function(productId, categoryId){
        var strPost = 'productId='+productId;
        strPost+='&categoryId='+categoryId;
        var strUrl = URL+'/saveProductToShopCat/?ajax=1';
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.addToCatSuccess,
            failure: MyController.asyncRequestFalse
        },strPost);
    },

    addToCatSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        Dom.get('product_order_'+aryData.productId).innerHTML = aryData.strContent;
        if(Dom.get('myShopCat')){
            Dom.get('myShopCat').innerHTML = aryData.myShopCat;
        }
        
    },

    changeOrderItem:function(orderId){
        var item = Dom.get('input_num_item_'+orderId).value;
        var price = Dom.get('hide_price_item_'+orderId).value;
        var strPost = 'orderId='+orderId;
        strPost+='&item='+item;
        strPost+='&price='+price;
        var strUrl = URL+'/changeOrderItem/?ajax=1';
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.changeOrderItemSuccess,
            failure: MyController.asyncRequestFalse
        },strPost);
    },
   
    changeOrderItemSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        Dom.get('myShopCat').innerHTML = aryData.leftOrderContent;
        Dom.get('listOrderItem').innerHTML = aryData.listOrder;
    },

    saveOrderInfo:function(){
        MyController.clearError();
        var strUrl = URL+'/saveOrderInfo/?ajax=1';
        YAHOO.util.Connect.setForm('orderInfo');
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.saveOrderInfoSuccess,
            failure: MyController.asyncRequestFalse
        });
        YAHOO.util.Connect.resetFormState();
    },

    saveOrderInfoSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        if(aryData.intOk == 1){
            Dom.get('shop_content').innerHTML = aryData.shopContent;
            Dom.get('myShopCat').innerHTML = aryData.shopLeftContent;
        }else{
            alert(aryData.messageError);
            MyController.innerError(aryData.aryError);
        }
    },

    saveregisterCustomer:function(){
        MyController.clearError();
        var strUrl = URL+'/maccathue/registerCustomer/?ajax=1';
        YAHOO.util.Connect.setForm('UserRegisterForm');
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.saveregisterCustomerSuccess,
            failure: MyController.asyncRequestFalse
        });
        YAHOO.util.Connect.resetFormState();
    },

    saveregisterCustomerSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        if(aryData.intOk == 1){
            Dom.get('shop_content').innerHTML = aryData.shopContent;
            Dom.get('myShopCat').innerHTML = aryData.shopLeftContent;
        }else{
            alert(aryData.messageError);
            MyController.innerError(aryData.aryError);
        }
    },

    innerError:function(aryErrorData){

        for (var property in aryErrorData) {
            if (aryErrorData.hasOwnProperty(property)) {
                try{
                    document.getElementById(property).style.display = 'inline';
                    document.getElementById(property).innerHTML = aryErrorData[property];
                }catch(e){
                    alert(e.message);
                }
            }
        }
    },
    
    clearError:function(){
        var elementError = Dom.getElementsByClassName('validate_error');
        var errorLenght = elementError.length;
        for(var i = 0; i < errorLenght; i++){
            elementError[i].style.display = 'none';
        }
    },
    showOverlaySuccess:function(messageSuccess){
        Dom.get('confirm-header').innerHTML = 'Thành công';
        Dom.get('confirm-message').innerHTML = messageSuccess;
        MyController.showOverlayConfirm();
    },
    showOverlayError:function(messageError){
        Dom.get('confirm-header').innerHTML = 'Có lỗi xảy ra';
        Dom.get('confirm-message').innerHTML = messageError;
        MyController.showOverlayConfirm();
    },
    showOverlayConfirm:function(){
        var x = 450;
        var y = 100;
        MyController.overlayConfirm = new YAHOO.widget.Panel('overlayConfirm',
        {
            //            xy:[x,y],
            fixedcenter:true,
            close:false,
            visible:false,
            draggable:false,
            modal:true,
            width:"400px",
            zIndex: 255
        });

        // Render the Dialogs
        MyController.overlayConfirm.render(document.body);
        MyController.overlayConfirm.center();
        document.getElementById('overlayConfirm').style.display = 'block';
        MyController.overlayConfirm.show();
    },
    addRowItemCategory:function(itemId, hiddenId){
        var parentId = document.getElementById(itemId);
        var listInputHidden = document.getElementById(hiddenId);
        var newDiv = document.createElement('div');
        newDiv.id = 'item-'+countItem;
        newDiv.innerHTML = listInputHidden.innerHTML;
        parentId.appendChild(newDiv)
        countItem++;
    },

    removeRowItemCategory:function(itemId , parentId){
        var listItem = document.getElementById(parentId).getElementsByTagName('div');
        if(listItem.length == 1){
            return;
        }
        var currentId = itemId.parentNode.parentNode;
        var parent = document.getElementById(parentId);
        parent.removeChild(currentId);

    },

    sendLinkPrice:function(message, productId){
        Dom.get('product_id').value = productId;
        MyController.showOverlayCommon(message);
    },

    showOverlayCommon:function(message){
        Dom.get('confirm-header-common').innerHTML = message;
        MyController.overlayCommon();
    },

    overlayCommon:function(){
        var x = 450;
        var y = 100;
        MyController.overlayContent = new YAHOO.widget.Panel('overlayCommon',
        {
            //            xy:[x,y],
            fixedcenter:true,
            close:false,
            visible:false,
            draggable:false,
            modal:true,
            width:"500px",
            zIndex: 255
        });

        // Render the Dialogs
        MyController.overlayContent.render(document.body);
        MyController.overlayContent.center();
        document.getElementById('overlayCommon').style.display = 'block';
        MyController.overlayContent.show();
    },

    overlayLogin:function(){
        var x = 450;
        var y = 100;
        MyController.overlayLoginContent = new YAHOO.widget.Panel('overlayLogin',
        {
            //            xy:[x,y],
            fixedcenter:true,
            close:false,
            visible:false,
            draggable:false,
            modal:true,
            width:"400px",
            zIndex: 255
        });

        // Render the Dialogs
        MyController.overlayLoginContent.render(document.body);
        MyController.overlayLoginContent.center();
        document.getElementById('overlayLogin').style.display = 'block';
        MyController.overlayLoginContent.show();
    },


    

    closeOverlayConfirm:function(){
        MyController.overlayConfirm.hide();
    },

    closeOverlayCommon:function(){
        MyController.overlayContent.hide();
    },

    closeOverlayLogin:function(){
        MyController.overlayLoginContent.hide();
    },

    activeOrder:function(){
        var strUrl = URL+'/activeOrder/?ajax=1';
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.activeOrderSuccess,
            failure: MyController.asyncRequestFalse
        });
    },
    activeOrderSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        if(aryData.intOk == 1){
            alert(aryData.message);
            window.location.href = aryData.url;
        }
    },
    backOrder:function(){
        history.go(-1);
    },
    reloadOrder:function(){
        window.location.reload();
    },

    searchProduct:function(page){
        if(page == undefined){
            page = 1;
        }
        var strUrl = URL+'/searchProduct/?ajax=1&page='+page;
        YAHOO.util.Connect.setForm('frmSearchProduct');
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.searchProductSuccess,
            failure: MyController.asyncRequestFalse
        });
        YAHOO.util.Connect.resetFormState();
    },

    searchProductSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        if(aryData.intOk == 1){
            Dom.get('search_result').innerHTML = aryData.content;
        }else{
            Dom.get('search_result').innerHTML = '';
        }
    },
    deleteAllOrder:function(){
        if(confirm('Bạn chắc chắn muốn xóa toàn bộ giỏ hàng?') == false){
            return;
        }
        var strUrl = URL+'/deleteAllOrder/?ajax=1';
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.deleteAllOrderSuccess,
            failure: MyController.asyncRequestFalse
        });
    },
    deleteAllOrderSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        if(aryData.intOk == 1){
            Dom.get('myShopCat').innerHTML = aryData.shopContent;
            if(Dom.get('listOrderItem')){
                Dom.get('listOrderItem').innerHTML = aryData.listOrder;
            }
            alert('Giỏ hàng của bạn đã được xóa.');
        }
    },
    
    deleteOrderById:function(orderId){
        if(confirm('Bạn chắc chắn muốn xóa sản phẩm này khỏi giỏ hàng?') == false){
            return;
        }
        var strPost = 'orderId='+orderId;
        var strUrl = URL+'/deleteOrderById/?ajax=1';
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.deleteOrderByIdSuccess,
            failure: MyController.asyncRequestFalse
        },strPost);
    },
    
    deleteOrderByIdSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        if(aryData.intOk == 1){
            Dom.get('myShopCat').innerHTML = aryData.shopContent;
            Dom.get('listOrderItem').innerHTML = aryData.listOrder;
            alert('Sản phẩm đã được xóa khỏi giỏ hàng.');
        }
    },
    setOrder:function(orderType){
        var strPost = 'orderType='+orderType;
        var strUrl = URL+'/setOrderProduct/?ajax=1';
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.setOrderSuccess,
            failure: MyController.asyncRequestFalse
        },strPost);
    },
    setOrderSuccess:function(){
        window.location.reload();
    },
    submitLink:function(){
        var strUrl = URL+'/submitLink/?ajax=1';
        YAHOO.util.Connect.setForm('frmSendLink');
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.submitLinkSuccess,
            failure: MyController.asyncRequestFalse
        });
        YAHOO.util.Connect.resetFormState();
    },
    submitLinkSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        if(aryData.intOk == 1){
            MyController.closeOverlayCommon();
            MyController.showOverlaySuccess(aryData.messageSuccess);
        }else{
            MyController.showOverlayError(aryData.strError);
        }
    },
    getListProduct:function(type, val){
        var strUrl = URL+'/getListProduct/?listType='+type+'&value='+val;
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.getProductSuccess,
            failure: MyController.asyncRequestFalse
        });
    },

    commentsProduct:function(){
        MyController.clearError();
        var strUrl = URL+'/commentsProduct/?ajax=1';
        YAHOO.util.Connect.setForm('CommentsForm');
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.commentsProductSuccess,
            failure: MyController.asyncRequestFalse
        });
        YAHOO.util.Connect.resetFormState();
    },

    commentsProductSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        if(aryData.intOk == 1){
            //alert('Cám ơn bạn đã đóng góp ý kiến cho chúng tôi!');
            MyController.showOverlaySuccess(aryData.message);
            //MyController.resetForm('CommentsForm');
            window.location.reload();
        //MyController.resetForm('CommentsForm1');
        }else{
            MyController.innerError(aryData.aryError);
            MyController.showOverlayError(aryData.messageError);
            MyController.changeCaptcha();
            Dom.get('commentsCaptcha').value = '';
        }
    },

    resetForm:function(formId){
        document.getElementById(formId).reset();
    },

    OverlayChat:function(){
        var x = 450;
        var y = 100;
        MyController.OverlayChatContent = new YAHOO.widget.Panel('overlayChat',
        {
            //            xy:[x,y],
            fixedcenter:true,
            close:false,
            visible:false,
            draggable:false,
            modal:true,
            width:"400px",
            zIndex: 255
        });

        // Render the Dialogs
        MyController.OverlayChatContent.render(document.body);
        MyController.OverlayChatContent.center();
        document.getElementById('overlayChat').style.display = 'block';
        MyController.OverlayChatContent.show();
    },

    closeOverlayChat:function(){
        MyController.OverlayChatContent.hide();
    },

    changeCaptcha:function(){
        var Stamp = new Date();
        var second = Stamp.getSeconds()/1000;
        document.getElementById('verify_image').src = "../../lib/captcha.php?ran="+second;
    },

    getProductSuccess:function(){
        window.location.reload();
    },
    customerContact:function(){
        MyController.clearError();
        var strUrl = URL+'/customerContact/?ajax=1';
        YAHOO.util.Connect.setForm('frmCustomerContact');
        YAHOO.util.Connect.asyncRequest ('POST', strUrl, {
            success: MyController.customerContactSuccess,
            failure: MyController.asyncRequestFalse
        });
        YAHOO.util.Connect.resetFormState();
    },
    customerContactSuccess:function(xmlhttp){
        var strJsonData = xmlhttp.responseText;
        var aryData = Json.parse(strJsonData);
        if(aryData.intOk == 1){
            MyController.showOverlaySuccess(aryData.message);
            window.location.href = '/';
        }else{
            MyController.showOverlayError(aryData.messageError);
            MyController.changeCaptcha();
            Dom.get('captcha_code').value = '';
            MyController.innerError(aryData.aryError);
        }
    }
}


